package ${basepackage}.framework.base.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.util.ReflectionUtils;

/**
 * apache BeanUtils�ĵȼ��ֻ࣬�ǽ�check exception��Ϊuncheck exception
 * @author badqiu
 *
 */
<#include "/author.include">
public class BeanUtils {
	
	private static void handleReflectionException(Exception e) {
		ReflectionUtils.handleReflectionException(e);
	}
	
	public static Object cloneBean(Object bean){
		try {
			return org.apache.commons.beanutils.BeanUtils.cloneBean(bean);
		} catch (Exception e) {
			handleReflectionException(e);
			return null;
		}
	}

	public static <T> T copyProperties(Class<T> destClass,Object orig) {
		try {
			Object target = destClass.newInstance();
			copyProperties((Object)target, orig);
			return (T)target;
		}catch(Exception e) {
			handleReflectionException(e);
			return null;
		}
	}
	
	public static void copyProperties(Object dest, Object orig) {
		try {
			org.apache.commons.beanutils.BeanUtils.copyProperties(dest, orig);
		} catch (Exception e) {
			handleReflectionException(e);
		}
	}

	public static void copyProperty(Object bean, String name, Object value) {
		try {
			org.apache.commons.beanutils.BeanUtils.copyProperty(bean, name, value);
		} catch (Exception e) {
			handleReflectionException(e);
		}
	}

	public static Map describe(Object bean) {
		try {
			return org.apache.commons.beanutils.BeanUtils.describe(bean);
		} catch (Exception e) {
			handleReflectionException(e);
			return null;
		}
	}

	public static String[] getArrayProperty(Object bean, String name){
		try {
			return org.apache.commons.beanutils.BeanUtils.getArrayProperty(bean, name);
		} catch (Exception e) {
			handleReflectionException(e);
			return null;
		}
	}

	public static String getIndexedProperty(Object bean, String name, int index){
		try {
			return org.apache.commons.beanutils.BeanUtils.getIndexedProperty(bean, name, index);
		} catch (Exception e) {
			handleReflectionException(e);
			return null;
		}
	}

	public static String getIndexedProperty(Object bean, String name){
		try {
			return org.apache.commons.beanutils.BeanUtils.getIndexedProperty(bean, name);
		} catch (Exception e) {
			handleReflectionException(e);
			return null;
		}
	}

	public static String getMappedProperty(Object bean, String name, String key){
		try {
			return org.apache.commons.beanutils.BeanUtils.getMappedProperty(bean, name, key);
		} catch (Exception e) {
			handleReflectionException(e);
			return null;
		}
	}

	public static String getMappedProperty(Object bean, String name){
		try {
			return org.apache.commons.beanutils.BeanUtils.getMappedProperty(bean, name);
		} catch (Exception e) {
			handleReflectionException(e);
			return null;
		}
	}

	public static String getNestedProperty(Object bean, String name){
		try {
			return org.apache.commons.beanutils.BeanUtils.getNestedProperty(bean, name);
		} catch (Exception e) {
			handleReflectionException(e);
			return null;
		}
	}

	public static String getProperty(Object bean, String name){
		try {
			return org.apache.commons.beanutils.BeanUtils.getProperty(bean, name);
		} catch (Exception e) {
			handleReflectionException(e);
			return null;
		}
	}

	public static String getSimpleProperty(Object bean, String name) {
		try {
			return org.apache.commons.beanutils.BeanUtils.getSimpleProperty(bean, name);
		} catch (Exception e) {
			handleReflectionException(e);
			return null;
		}
	}

	public static void populate(Object bean, Map properties) {
		try {
			org.apache.commons.beanutils.BeanUtils.populate(bean, properties);
		} catch (Exception e) {
			handleReflectionException(e);
		}
	}

	public static void setProperty(Object bean, String name, Object value) {
		try {
			org.apache.commons.beanutils.BeanUtils.setProperty(bean, name, value);
		} catch (Exception e) {
			handleReflectionException(e);
		}
	}
	
	public static String printObject(Object o){
		StringBuffer sb = new StringBuffer();
		
		try {
			Field[] fields = o.getClass().getDeclaredFields();
			sb.append(o.getClass().getSimpleName() + "= [");
			for(int i = 0;i<fields.length;i++){
				String fieldName = fields[i].getName();
				if(fieldName.equals("serialVersionUID")){
					continue;
				}
				String firstChar = fieldName.substring(0, 1);
				
				String getMethodName = "get" + firstChar.toUpperCase() + fieldName.substring(1);
				
				Object value;

				try {
					Method getMethod = o.getClass().getMethod(getMethodName, new Class[]{});
					
					value = getMethod.invoke(o);
					sb.append(fieldName + " = " + value + ",");
				} catch (Exception e) {					
				}

				
				
			}
			
			Class superClass = o.getClass().getSuperclass();
			while(superClass != null){
				fields = superClass.getDeclaredFields();
				for(int i = 0;i<fields.length;i++){
					String fieldName = fields[i].getName();
					if(fieldName.equals("serialVersionUID")){
						continue;
					}
					String firstChar = fieldName.substring(0, 1);
					
					String getMethodName = "get" + firstChar.toUpperCase() + fieldName.substring(1);
					
					Object value;

					try {
						Method getMethod = superClass.getMethod(getMethodName, new Class[]{});
						
						value = getMethod.invoke(o);
						sb.append(fieldName + " = " + value + ",");
					} catch (Exception e) {					
					}
				}
				
				superClass = superClass.getSuperclass();
				
				
			}
			sb.append(" ]");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
		
		
	}

}
